﻿using System.Collections.Generic;
using Dogshow.Model;
using DogshowRepository;
using System.Linq;

namespace Dogshow.Linq2SQLRepository
{
    public class KinoloskaDrustvaRepository : RepositoryBase, IKinoloskaDrustvaRepository
    {
        public int Sacuvaj(KinoloskoDrustvo entity)
        {
            using (var model = new DogshowModel())
            {
                KinoloskaDrustva kd;
                if (entity.Id == 0)
                {
                    kd = ModelMapper.Map(entity);
                    model.KinoloskaDrustvas.InsertOnSubmit(kd);
                }
                else
                {
                    kd = model.KinoloskaDrustvas.Single(x => x.Id == entity.Id);
                    ModelMapper.Map(entity, kd);
                }
                model.SubmitChanges();
                return kd.Id; 
            }
        }

        public IEnumerable<KinoloskoDrustvo> VratiSve()
        {
            using (var model = new DogshowModel())
            {
                return model.KinoloskaDrustvas.Select(ModelMapper.Map).ToArray(); 
            }
        }

        public KinoloskoDrustvo VratiPoId(int id)
        {
            using (var model = new DogshowModel())
            {
                var kd = model.KinoloskaDrustvas.SingleOrDefault(x => x.Id == id);
                if (kd == null) return null;
                return ModelMapper.Map(kd); 
            }
        }
    }
}